package com.gpframework.module.controller.manufacture;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.manufacture.domain.GpProduceProduct;
import com.gpframework.module.manufacture.domain.req.GpProduceProductAdd;
import com.gpframework.module.manufacture.domain.req.GpProduceProductReq;
import com.gpframework.module.manufacture.domain.req.GpProduceProductUpdate;
import com.gpframework.module.manufacture.domain.vo.GpProduceProductVo;
import com.gpframework.module.manufacture.service.IGpProduceProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpProduceProductController
 * @Author gpframework
 * @Date 2023-11-10
 * @Version 1.0.0
 * @Description 生产产品基础Controller
 */
@Validated
@Api(tags = "生产产品基础")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/manufacture/produceProduct" )
public class GpProduceProductController extends BaseController {

    private final IGpProduceProductService iGpProduceProductService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-11-10
  * @Param gpProduceProduct
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    //@PreAuthorize("@ss.hasPermi('manufacture:produceProduct:list')")
    @GetMapping("/list")
    public TableDataInfo<GpProduceProductVo> list(GpProduceProductReq gpProduceProductReq)
    {
        startPage();
        QueryWrapper<GpProduceProduct> lqw = new QueryWrapper<GpProduceProduct>();
        gpProduceProductReq.generatorQuery(lqw,true);
        List<GpProduceProductVo> list = iGpProduceProductService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpProduceProduct
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('manufacture:produceProduct:add')" )
    @Log(title = "添加生产产品基础" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProduceProductAdd gpProduceProductAdd) {
        GpProduceProduct gpProduceProduct = Convert.convert(new TypeReference<GpProduceProduct>() {}, gpProduceProductAdd);
        return toAjax(iGpProduceProductService.save(gpProduceProduct) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpProduceProduct
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('manufacture:produceProduct:edit')" )
    @Log(title = "修改生产产品基础" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpProduceProductUpdate gpProduceProductUpdate) {
        GpProduceProduct gpProduceProduct = Convert.convert(new TypeReference<GpProduceProduct>() {}, gpProduceProductUpdate);
        return toAjax(iGpProduceProductService.updateById(gpProduceProduct) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpProduceProduct
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('manufacture:produceProduct:export')" )
    @Log(title = "导出生产产品基础" , businessType = BusinessType.EXPORT)
    @ApiModelProperty("/导出生产产品基础" )
    @GetMapping("/export" )
    public AjaxResult export(GpProduceProductReq gpProduceProductReq) {
        QueryWrapper<GpProduceProduct> lqw = new QueryWrapper<GpProduceProduct>();
        gpProduceProductReq.generatorQuery(lqw,true);
        List<GpProduceProductVo> list = iGpProduceProductService.listVo(lqw);
        ExcelUtil<GpProduceProductVo> util = new ExcelUtil<GpProduceProductVo>(GpProduceProductVo. class);
        return util.exportExcel(list, "produceProduct" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpProduceProduct
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('manufacture:produceProduct:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProduceProductVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProduceProductService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpProduceProduct
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('manufacture:produceProduct:remove')" )
    @Log(title = "删除生产产品基础" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProduceProductService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("下载模板")
    @PreAuthorize("@ss.hasPermi('order:produceProduct:export')" )
    @Log(title = "下载模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult importTemplate() {
        ExcelUtil<GpProduceProductVo> util = new ExcelUtil<GpProduceProductVo>(GpProduceProductVo.class);
        return util.importTemplateExcel("生产产品基础表");
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('manufacture:produceProduct:add')" )
    @Log(title = "导入" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 删除旧数据
        iGpProduceProductService.remove(new QueryWrapper<GpProduceProduct>().eq("del_status", YesNoEnum.NO.getKey()));
        // 导入新数据
        ExcelUtil<GpProduceProductVo> util = new ExcelUtil<GpProduceProductVo>(GpProduceProductVo.class);
        List<GpProduceProductVo> excelList = util.importCacheExcel(file.getInputStream());
        List<GpProduceProduct> list = Convert.convert(new TypeReference<List<GpProduceProduct>>() {}, excelList);
        return iGpProduceProductService.saveBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }
}
